package service

import (
	"math"
)

// SemanticAnalyzer 语义分析器
type SemanticAnalyzer struct {
	embeddingModel EmbeddingModel
}

// NewSemanticAnalyzer 创建语义分析器
func NewSemanticAnalyzer(embeddingModel EmbeddingModel) *SemanticAnalyzer {
	return &SemanticAnalyzer{
		embeddingModel: embeddingModel,
	}
}

// CalculateCosineSimilarity 计算余弦相似度
func (s *SemanticAnalyzer) CalculateCosineSimilarity(vec1, vec2 []float32) float32 {
	if len(vec1) != len(vec2) || len(vec1) == 0 {
		return 0.0
	}
	
	var dotProduct, norm1, norm2 float32
	
	for i := 0; i < len(vec1); i++ {
		dotProduct += vec1[i] * vec2[i]
		norm1 += vec1[i] * vec1[i]
		norm2 += vec2[i] * vec2[i]
	}
	
	if norm1 == 0 || norm2 == 0 {
		return 0.0
	}
	
	return dotProduct / (sqrt32(norm1) * sqrt32(norm2))
}

// sqrt32 计算float32的平方根
func sqrt32(x float32) float32 {
	return float32(math.Sqrt(float64(x)))
}